package com.dianping.cat.home.router.transform;

import com.dianping.cat.home.router.IVisitor;
import com.dianping.cat.home.router.entity.DefaultServer;
import com.dianping.cat.home.router.entity.Domain;
import com.dianping.cat.home.router.entity.Group;
import com.dianping.cat.home.router.entity.GroupServer;
import com.dianping.cat.home.router.entity.Network;
import com.dianping.cat.home.router.entity.NetworkPolicy;
import com.dianping.cat.home.router.entity.RouterConfig;
import com.dianping.cat.home.router.entity.Server;
import com.dianping.cat.home.router.entity.ServerGroup;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;
  
/*  26 */   public DefaultNativeBuilder(OutputStream out) { this(out, null); }

  
  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
/*  30 */     this.m_out = new DataOutputStream(out);
    this.m_visitor = (visitor == null) ? this : visitor;
  }
  
  public static byte[] build(RouterConfig routerConfig) {
/*  35 */     ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

/*  37 */     build(routerConfig, out);
/*  38 */     return out.toByteArray();
  }

  
/*  42 */   public static void build(RouterConfig routerConfig, OutputStream out) { routerConfig.accept(new DefaultNativeBuilder(out)); }


  
  public void visitDefaultServer(DefaultServer defaultServer) {
/*  47 */     if (defaultServer.getId() != null) {
/*  48 */       writeTag(1, 1);
/*  49 */       writeString(defaultServer.getId());
    }
    
/*  52 */     writeTag(2, 0);
/*  53 */     writeDouble(defaultServer.getWeight());

/*  55 */     writeTag(3, 0);
/*  56 */     writeInt(defaultServer.getPort());

/*  58 */     writeTag(4, 0);
/*  59 */     writeBoolean(defaultServer.getEnable());

/*  61 */     writeTag(63, 3);
  }

  
  public void visitDomain(Domain domain) {
/*  66 */     if (domain.getId() != null) {
/*  67 */       writeTag(1, 1);
/*  68 */       writeString(domain.getId());
    }
    
/*  71 */     if (!domain.getGroups().isEmpty()) {
/*  72 */       writeTag(33, 2);
/*  73 */       writeInt(domain.getGroups().size());

/*  75 */       for (Group group : domain.getGroups().values()) {
/*  76 */         group.accept(this.m_visitor);
      }
    }
    
/*  80 */     writeTag(63, 3);
  }

  
  public void visitGroup(Group group) {
/*  85 */     if (group.getId() != null) {
/*  86 */       writeTag(1, 1);
/*  87 */       writeString(group.getId());
    }
    
/*  90 */     if (!group.getServers().isEmpty()) {
/*  91 */       writeTag(33, 2);
/*  92 */       writeInt(group.getServers().size());

/*  94 */       for (Server server : group.getServers()) {
        server.accept(this.m_visitor);
      }
    }
    
/*  99 */     writeTag(63, 3);
  }

  
  public void visitGroupServer(GroupServer groupServer) {
/* 104 */     if (groupServer.getId() != null) {
/* 105 */       writeTag(1, 1);
/* 106 */       writeString(groupServer.getId());
    }
    
/* 109 */     writeTag(63, 3);
  }

  
  public void visitNetwork(Network network) {
/* 114 */     if (network.getId() != null) {
/* 115 */       writeTag(1, 1);
/* 116 */       writeString(network.getId());
    }
    
/* 119 */     writeTag(63, 3);
  }

  
  public void visitNetworkPolicy(NetworkPolicy networkPolicy) {
/* 124 */     if (networkPolicy.getId() != null) {
/* 125 */       writeTag(1, 1);
/* 126 */       writeString(networkPolicy.getId());
    }
    
/* 129 */     if (networkPolicy.getTitle() != null) {
/* 130 */       writeTag(2, 1);
/* 131 */       writeString(networkPolicy.getTitle());
    }
    
/* 134 */     writeTag(3, 0);
/* 135 */     writeBoolean(networkPolicy.getBlock());

/* 137 */     if (networkPolicy.getServerGroup() != null) {
/* 138 */       writeTag(4, 1);
/* 139 */       writeString(networkPolicy.getServerGroup());
    }
    
/* 142 */     if (!networkPolicy.getNetworks().isEmpty()) {
/* 143 */       writeTag(33, 2);
/* 144 */       writeInt(networkPolicy.getNetworks().size());

/* 146 */       for (Network network : networkPolicy.getNetworks().values()) {
/* 147 */         network.accept(this.m_visitor);
      }
    }
    
/* 151 */     writeTag(63, 3);
  }

  
  public void visitRouterConfig(RouterConfig routerConfig) {
/* 156 */     writeTag(63, 0);

/* 158 */     if (routerConfig.getBackupServer() != null) {
/* 159 */       writeTag(1, 1);
/* 160 */       writeString(routerConfig.getBackupServer());
    }
    
/* 163 */     writeTag(2, 0);
/* 164 */     writeInt(routerConfig.getBackupServerPort());

/* 166 */     if (routerConfig.getStartTime() != null) {
/* 167 */       writeTag(3, 1);
/* 168 */       writeDate(routerConfig.getStartTime());
    }
    
/* 171 */     if (routerConfig.getDomain() != null) {
/* 172 */       writeTag(4, 1);
/* 173 */       writeString(routerConfig.getDomain());
    }
    
/* 176 */     if (routerConfig.getEndTime() != null) {
/* 177 */       writeTag(5, 1);
/* 178 */       writeDate(routerConfig.getEndTime());
    }
    
/* 181 */     if (!routerConfig.getDefaultServers().isEmpty()) {
/* 182 */       writeTag(33, 2);
/* 183 */       writeInt(routerConfig.getDefaultServers().size());

/* 185 */       for (DefaultServer defaultServer : routerConfig.getDefaultServers().values()) {
/* 186 */         defaultServer.accept(this.m_visitor);
      }
    }
    
/* 190 */     if (!routerConfig.getNetworkPolicies().isEmpty()) {
/* 191 */       writeTag(34, 2);
/* 192 */       writeInt(routerConfig.getNetworkPolicies().size());

/* 194 */       for (NetworkPolicy networkPolicy : routerConfig.getNetworkPolicies().values()) {
/* 195 */         networkPolicy.accept(this.m_visitor);
      }
    }
    
/* 199 */     if (!routerConfig.getServerGroups().isEmpty()) {
/* 200 */       writeTag(35, 2);
/* 201 */       writeInt(routerConfig.getServerGroups().size());

/* 203 */       for (ServerGroup serverGroup : routerConfig.getServerGroups().values()) {
/* 204 */         serverGroup.accept(this.m_visitor);
      }
    }
    
/* 208 */     if (!routerConfig.getDomains().isEmpty()) {
/* 209 */       writeTag(36, 2);
/* 210 */       writeInt(routerConfig.getDomains().size());

/* 212 */       for (Domain domain : routerConfig.getDomains().values()) {
/* 213 */         domain.accept(this.m_visitor);
      }
    }
    
/* 217 */     writeTag(63, 3);
  }

  
  public void visitServer(Server server) {
/* 222 */     if (server.getId() != null) {
/* 223 */       writeTag(1, 1);
/* 224 */       writeString(server.getId());
    }
    
/* 227 */     writeTag(2, 0);
/* 228 */     writeInt(server.getPort());

/* 230 */     writeTag(3, 0);
/* 231 */     writeDouble(server.getWeight());

/* 233 */     writeTag(63, 3);
  }

  
  public void visitServerGroup(ServerGroup serverGroup) {
/* 238 */     if (serverGroup.getId() != null) {
/* 239 */       writeTag(1, 1);
/* 240 */       writeString(serverGroup.getId());
    }
    
/* 243 */     if (serverGroup.getTitle() != null) {
/* 244 */       writeTag(2, 1);
/* 245 */       writeString(serverGroup.getTitle());
    }
    
/* 248 */     if (!serverGroup.getGroupServers().isEmpty()) {
/* 249 */       writeTag(33, 2);
/* 250 */       writeInt(serverGroup.getGroupServers().size());

/* 252 */       for (GroupServer groupServer : serverGroup.getGroupServers().values()) {
/* 253 */         groupServer.accept(this.m_visitor);
      }
    }
    
/* 257 */     writeTag(63, 3);
  }
  
  private void writeBoolean(boolean value) {
    try {
/* 262 */       this.m_out.writeByte(value ? 1 : 0);
/* 263 */     } catch (IOException e) {
/* 264 */       throw new RuntimeException(e);
    }
  }
  
  private void writeDate(Date value) {
    try {
/* 270 */       writeVarint(value.getTime());
/* 271 */     } catch (IOException e) {
/* 272 */       throw new RuntimeException(e);
    }
  }
  
  private void writeDouble(double value) {
    try {
/* 278 */       writeVarint(Double.doubleToLongBits(value));
/* 279 */     } catch (IOException e) {
/* 280 */       throw new RuntimeException(e);
    }
  }
  
  private void writeInt(int value) {
    try {
/* 286 */       writeVarint(value & 0xFFFFFFFFL);
/* 287 */     } catch (IOException e) {
/* 288 */       throw new RuntimeException(e);
    }
  }
  
  private void writeString(String value) {
    try {
/* 294 */       this.m_out.writeUTF(value);
/* 295 */     } catch (IOException e) {
/* 296 */       throw new RuntimeException(e);
    }
  }
  
  private void writeTag(int field, int type) {
    try {
/* 302 */       this.m_out.writeByte((field << 2) + type);
/* 303 */     } catch (IOException e) {
/* 304 */       throw new RuntimeException(e);
    }
  }
  
  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 310 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 311 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 314 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 315 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/router/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */